package com.xiaoyu.awakening.common.core.kit;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;

public class PdfBoxKit {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\阅读\\03331公共事业管理-电子教辅.pdf");
        try {
            PDDocument document = Loader.loadPDF(file);
            PDFTextStripper stripper = new PDFTextStripper();
            //按页进行读取，页码从1开始
            stripper.setStartPage(1);
            stripper.setEndPage(5);
            //按位置进行排序
            stripper.setSortByPosition(true);
            //获取文本
            String text = stripper.getText(document);
            System.out.println(text);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
